package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.common.result.AjaxResult;
import com.crm.common.result.PageResult;
import com.crm.common.utils.StringUtils;
import com.crm.domain.DictData;
import com.crm.domain.Transfer;
import com.crm.dto.TransferPageQueryDto;
import com.crm.mapper.DictDataMapper;
import com.crm.mapper.TransferMapper;
import com.crm.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class TransferServiceImpl extends ServiceImpl<TransferMapper, Transfer> implements TransferService {
    @Override
    public PageResult listpage(TransferPageQueryDto queryDto) {
        Page<Transfer> page = new Page<>(queryDto.getPageNum(), queryDto.getPageSize());
        LambdaQueryWrapper<Transfer> wrapper = new LambdaQueryWrapper<>();

        if (queryDto.getEndTime() == null) {
            queryDto.setEndTime(LocalDateTime.now());
        }

        if (queryDto.getCreateTime() != null) {
            wrapper.between(Transfer::getCreateTime, queryDto.getCreateTime(), queryDto.getEndTime());
        }

        if (StringUtils.isNotBlank(queryDto.getUserName())) {
            wrapper.like(Transfer::getUserName, queryDto.getUserName());
        }

        if (StringUtils.isNotBlank(queryDto.getPhonenumber())) {
            wrapper.like(Transfer::getPhonenumber, queryDto.getPhonenumber());
        }

        super.page(page, wrapper);

        List<Transfer> records = page.getRecords();
        long total = page.getTotal(); //总记录数应从分页对象中获取

        return new PageResult(total, records);
    }

}
